﻿#include <stdio.h>
#include <string.h>

// 7-1 两数之和
void ex8_7_1()
{
	int n, target;
	int a[100];
	scanf("%d%d", &n, &target);

	for (int i = 0; i < n; i++)
	{
		scanf("%d", a + i);
	}

	for (int i = 0; i < n; i++)
		for (int j = i + 1; j < n; j++)
		{
			if (a[i] + a[j] == target)
			{
				printf("%d %d", i, j);
				return;
			}
		}
	printf("-1 -1");
}

// 7-2 循环数组
void reverse(int r[], int begin, int end)
{
	int i, temp;
	for (i = 0; i < (end - begin + 1) / 2; i++)
	{
		temp = r[begin + i];
		r[begin + i] = r[end - i];
		r[end - i] = temp;
	}
}

void ex8_7_2()
{
	int a[100];
	int n, k;
	scanf("%d%d", &n, &k);

	k %= n;

	for (int i = 0; i < n; i++)
	{
		scanf("%d", a + i);
	}

	reverse(a, 0, n - 1); // 全部逆置

	reverse(a, 0, n - k - 1); // 前n-p个元素逆置

	reverse(a, n - k, n - 1); // 后p个元素逆置

	for (int i = 0; i < n; i++)
		printf("%d ", a[i]);
}

// 7-3 数组区间
void ex8_7_3()
{
	int a[10001];
	int sum[10001];
	int n, k;
	int begin, end;

	scanf("%d%d", &n, &k);
	sum[0] = a[0] = 0;

	for (int i = 1; i <= n; i++)
	{
		scanf("%d", a + i);
		sum[i] = sum[i - 1] + a[i];
	}

	for (int i = 0; i < k; i++)
	{
		scanf("%d%d", &begin, &end);
		printf("%d\n", sum[end + 1] - sum[begin]);
	}
}
// 7-4 两数相加
void reverse_str(char str[])
{
	int n = strlen(str);
	char c;
	for (int i = 0; i < n / 2; i++)
	{
		c = str[i];
		str[i] = str[n - i - 1];
		str[n - i - 1] = c;
	}
}
void ex8_7_4()
{
	char a[10002], b[10002];
	int c[10002];
	int carry = 0;
	int na = 0, nb = 0, i = 0;

	scanf("%s%s", a, b);

	na = strlen(a), nb = strlen(b);
	reverse_str(a);
	reverse_str(b);
	while (i < na || i < nb)
	{
		c[i] = (i < na ? (a[i] - '0') : 0) + (i < nb ? (b[i] - '0') : 0) + carry;
		carry = c[i] / 10;
		c[i] %= 10;
		i++;
	}
	if (carry)
		c[i++] = carry;
	for (int j = i - 1; j >= 0; j--)
		printf("%d", c[j]);
}

// 7-5 生命游戏
int in[502][502] = {0};
int out[502][502] = {0};

int n;

void init_board()
{
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			scanf("%d", &in[i][j]);
		}
	}
}

void print_board()
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			printf("%d ", out[i][j]);
		}
		printf("\n");
	}
}

void gameOfLife()
{
	int sum = 0;
	for (int i = 1; i < n - 1; i++)
	{
		for (int j = 1; j < n - 1; j++)
		{
			sum = 0;
			sum = in[i - 1][j - 1] + in[i - 1][j] + in[i - 1][j + 1] + in[i][j - 1] + in[i][j + 1] + in[i + 1][j - 1] + in[i + 1][j] + in[i + 1][j + 1];
			out[i][j] = in[i][j];
			if (sum == 3)
			{
				out[i][j] = 1;
			}
			else if (sum < 2 || sum > 3)
			{
				out[i][j] = 0;
			}
		}
	}
}

void ex8_7_5()
{
	init_board();
	gameOfLife();
	print_board();
}

int main()
{
}